package com.ltv.xiangqibook.txttool;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;

public class TxtGenerator {
    public static void main(String[] args) {
        try {
            list(new File(args[0]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void list(File file) throws Exception {

            File[] children = file.listFiles();

            File txt = null;
            FileOutputStream fos = null;
            if (children != null) {
                Arrays.sort(children);
                for (File child : children) {
                    if (child.isFile() && child.getName().endsWith(".pgn")) {
                        txt = new File(child.getAbsolutePath().replace(".pgn", ".txt"));
                        if (!txt.exists()) {
                            txt.createNewFile();
                            
                            StringBuffer sb = new StringBuffer();
                            sb.append("[start_index]\n");
                            sb.append("[/start_index]\n");
                            
                            fos = new FileOutputStream(txt);
                            fos.write(sb.toString().getBytes());
                            fos.close();
                        }
                    }
                }
            }
        }
}
